import 'package:audio_output_device/audio_output_device.dart';
import 'package:flutter/material.dart';

void main() {
  runApp(const MyApp());
}

class MyApp extends StatelessWidget {
  const MyApp({super.key});

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      theme: ThemeData(
        colorScheme: ColorScheme.fromSeed(seedColor: Colors.deepPurple),
        useMaterial3: true,
      ),
      home: const MyHomePage(title: 'Flutter Demo Home Page'),
    );
  }
}

class MyHomePage extends StatefulWidget {
  const MyHomePage({super.key, required this.title});
  final String title;

  @override
  State<MyHomePage> createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage>{

  final AudioOutputDevice _audioOutputDevice = AudioOutputDevice();

  @override
  void initState() {
    super.initState();
    _audioOutputDevice.enableLog(enable: true);
    WidgetsBinding.instance.addPostFrameCallback((_){
      if(mounted){
        _audioOutputDevice.initAudioSession();
      }
    });
  }

  void _getAudioDevices(){
    _audioOutputDevice.getAudioDevices();
  }

  @override
  void dispose() {
    _audioOutputDevice.close();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        backgroundColor: Theme.of(context).colorScheme.inversePrimary,
        title: Text(widget.title),
      ),
      body: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: <Widget>[
            const Text(
              'You have pushed the button this many times:',
            ),
          ],
        ),
      ),
      floatingActionButton: FloatingActionButton(
        onPressed: _getAudioDevices,
        child: const Icon(Icons.add),
      ),
    );
  }
}
